/**
 * 
 */
package jvs.peer.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Properties;

import jvs.peer.io.ChannelManager;
import jvs.peer.net.PeerAddress;
import jvs.peer.net.PeerSocket;

/**
 * @author qiangli
 * 
 */
public class EchoClient {
	private PeerAddress peer = null;

	/**
	 * 
	 */
	public EchoClient(PeerAddress peer) {
		this.peer = peer;
	}

	public void echo(File src, File dst) {

		try {
			Socket socket = new PeerSocket(peer);

			OutputStream sos = socket.getOutputStream();
			InputStream sis = socket.getInputStream();

			FileInputStream fis = new FileInputStream(src);
			FileOutputStream fos = new FileOutputStream(dst);

			Thread outbound = new Copier(fis, sos);
			Thread inbound = new Copier(sis, fos);

			outbound.start();
			inbound.start();

			// socket.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void echo() {
		try {
			Socket socket = null;
			PrintWriter out = null;
			BufferedReader in = null;

			socket = new PeerSocket(peer);
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			BufferedReader stdin = new BufferedReader(new InputStreamReader(
					System.in));
			String input = null;

			while ((input = stdin.readLine()) != null) {
				out.println(input);
				out.flush();
				System.err.println("echo: " + in.readLine());

				if (input.equalsIgnoreCase("bye")) {
					break;
				}
			}

			out.close();
			in.close();
			stdin.close();
			socket.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Properties attrs = System.getProperties();
			attrs.load(new FileInputStream("C:/workspace/jvs2/resource/user1.txt"));
			
			ChannelManager.getInstance().init(attrs);
			ChannelManager.getInstance().startup();

			final PeerAddress peer = new PeerAddress(attrs.getProperty("peer"));
			
			String[] files = new String[] {"song.mp3","image.jpg","text.txt"};
			for (final String file: files) {
				Thread t = new Thread() {
					public void run() {
						EchoClient client = new EchoClient(peer);
						
						File src = new File("C:/temp/"+file);
						File dst = new File("C:/temp/out-"+file);
						client.echo(src, dst);
					}
				};
				t.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

class Copier extends Thread {
	private InputStream in;
	private OutputStream out;

	public Copier(InputStream in, OutputStream out) {
		this.in = in;
		this.out = out;
	}

	@Override
	public void run() {
		try {
			byte[] buf = new byte[2048];
			int count = 0;
			while (true) {
				int read = in.read(buf);
				if (read > 0) {
					count += read;
					out.write(buf, 0, read);
					out.flush();

					System.err.println(getName() + ": " + count);
				} else {
					break;
				}
			}

			out.close();
			in.close();

			System.err.println("Done! " + getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
